package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func main() {

	//客户端
	dial, err := net.Dial("tcp", "127.0.0.1:9193")

	if err != nil {
		fmt.Println("dial error", err)
	}

	reader := bufio.NewReader(os.Stdin)

	fmt.Println("输入内容，输入exit退出")

	for {

		message, err := reader.ReadString('\n')

		fmt.Println("请输入内容")

		if message == "exit" {
			closeConnection(dial)
			break
		}

		_, err = dial.Write([]byte(message))

		if err != nil {
			fmt.Println("write error", err)
		}

		buffer := make([]byte, 1024)

		cnt, err := dial.Read(buffer)

		if err != nil {
			fmt.Println("read error", err)
		}

		fmt.Println("read message from server", string(buffer[0:cnt-1]))

	}

}

func closeConnection(dial net.Conn) {
	if err := dial.Close(); err != nil {
		fmt.Println("close error")
	}
}
